

var Misc = {

  _TYPE_IN            : "IN",
  _TYPE_OUT           : "OUT",
  _UNKNOWN            : "?",
  _SUPPLIER           : "Supplier",
  _CUSTOMER           : "Customer",
  _COMMA_SEPARATOR    : ",",
  _CHECK_TRUE         : "true",
  _CHECK_FALSE        : "false",
  _CASH_BANK_IN       : "IN",
  _CASH_BANK_OUT      : "OUT",
  _CASH_BANK_INVOICE  : "Invoice",
  _CASH_BANK_CCL_DP   : "DP Cancel", 

  equivalen : function (net,
                        rate,
                        amount){
  
    net    = document.getElementsByName(net);
    rate   = document.getElementsByName(rate);
    amount = document.getElementsByName(amount);
    
    net    = GMisc.firstElement(net);
    rate   = GMisc.firstElement(rate);
    amount = GMisc.firstElement(amount);
    
    var tmp = parseFloat(rate.value) * parseFloat(amount.value);
     
    net.value = tmp.toFixed(2);
  },

  procDrop : function (thisObj,
                       prevStatus,
                       amountEnt,
                       totAmount,
                       netEnt,
                       totNetEnt){
  
    prevStatus    = document.getElementsByName(prevStatus);
    
    prevStatus    = GMisc.firstElement(prevStatus);
    
    
    if(thisObj.checked == true){
    
      GMisc.syncVal2F(amountEnt,
                      totAmount);
      
      GMisc.syncVal2F(netEnt,
                      totNetEnt);
    
      prevStatus.value = this._CHECK_TRUE;
    }else{
    
      /**
      * Checked if previous checked is true
      */
      if(prevStatus.value == this._CHECK_TRUE){
      
        GMisc.sumVal2F(amountEnt,
                       totAmount);
        
        GMisc.sumVal2F(netEnt,
                       totNetEnt);
                       
        prevStatus.value = this._CHECK_FALSE;               
      }else{
      
        /*Do nothing*/
      }
    }
  },

  computeAll : function (totInvoice,
                         totPayment,
                         gainEnt,
                         discEnt){
                         
    totInvoice  = document.getElementsByName(totInvoice);
    totPayment  = document.getElementsByName(totPayment);
    gainEnt     = document.getElementsByName(gainEnt);
    discEnt     = document.getElementsByName(discEnt);
    
    totInvoice  = GMisc.firstElement(totInvoice);
    totPayment  = GMisc.firstElement(totPayment);
    gainEnt     = GMisc.firstElement(gainEnt);
    discEnt     = GMisc.firstElement(discEnt);
    
    var tmp = parseFloat(totPayment.value) -
              parseFloat(totInvoice.value) -
              parseFloat(gainEnt.value);
              
    discEnt.value = tmp.toFixed(2);
  },

  setInvoice : function (thisObj,
                         prevStatus,
                         srcInvoice){
  
    srcInvoice = document.getElementsByName(srcInvoice);
    prevStatus = document.getElementsByName(prevStatus);
    
    srcInvoice = GMisc.firstElement(srcInvoice);
    prevStatus = GMisc.firstElement(prevStatus);
    
    if(thisObj.checked == true){
    
      thisObj.value    = srcInvoice.value;
    
      prevStatus.value = this._CHECK_TRUE;
    }else{
    
      /**
      * Checked if previous checked is true
      */
      if(prevStatus.value == this._CHECK_TRUE){
      
        thisObj.value    = "";
                       
        prevStatus.value = this._CHECK_FALSE;               
      }else{
      
        /*Do nothing*/
      }
    }
    
  },

  txWindow : function (srcHostIp,
                       whoseName,
                       whoseCode,
                       currency,
                       rate,
                       totSrcNet,
                       isInOrOut,
                       tx,
                       cbCode){
    
    whoseName  = document.getElementsByName(whoseName);
    whoseCode  = document.getElementsByName(whoseCode);
    currency   = document.getElementsByName(currency);
    rate       = document.getElementsByName(rate);
    totSrcNet  = document.getElementsByName(totSrcNet);
    isInOrOut  = document.getElementsByName(isInOrOut);
    tx         = document.getElementsByName(tx);
    cbCode     = document.getElementsByName(cbCode);
    
    whoseName  = GMisc.firstElement(whoseName);
    whoseCode  = GMisc.firstElement(whoseCode);
    currency   = GMisc.firstElement(currency);
    rate       = GMisc.firstElement(rate);
    totSrcNet  = GMisc.firstElement(totSrcNet);
    isInOrOut  = GMisc.firstElement(isInOrOut);
    tx         = GMisc.firstElement(tx);
    cbCode     = GMisc.firstElement(cbCode);

    switch(isInOrOut.value){
    
      case this._CASH_BANK_IN:
      
        switch(tx.value){
        
          case this._CASH_BANK_INVOICE:
          
            var targetFile = "inputInvoice_html.php";
            break;
            
          case this._CASH_BANK_CCL_DP:
          
            var targetFile = "";
            break;
            
          default:
            
            /**
		         * Immediately exit()
		         */
		        return false;
        }
        break;
        
      case this._CASH_BANK_OUT:
      
        switch(tx.value){
        
          case this._CASH_BANK_INVOICE:
          
            var targetFile = "outputInvoice_html.php";
            break;
            
          case this._CASH_BANK_CCL_DP:
          
            var targetFile = "outDpCancel_html.php";
            break;
            
          default:
            
            /**
             * Immediately exit()
             */
            return false;
        }
        break;
        
      default:
        
        /**
         * Immediately exit()
         */
        return false;
    }
    
    var _BASE_HTML = "http://"                + srcHostIp  + 
                     "/ecs/tocmenu_31/views/" + targetFile ;
    
    /**
    * Fetch the opener properties
    *
    */
    var _HEIGHT = 600 + this._COMMA_SEPARATOR;
    var _WIDTH  = 800 + this._COMMA_SEPARATOR;
    
    hdlChild = window.open((_BASE_HTML       +
                            "?whoseName="    + whoseName.value +
                            "&whoseCode="    + whoseCode.value +
                            "&currency="     + currency.value  +
                            "&rate="         + rate.value      +
                            "&totSrcNet="    + totSrcNet.value +
                            "&C_B_CODE="     + cbCode.value),
                           "Invoice List",
                           ("height=" + _HEIGHT + 
                            "width="  + _WIDTH  + 
                            "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizeable=1"));
  
  },

  setSuppCst : function (type,
                         supplierCustomer){
  
    type             = document.getElementsByName(type);
    supplierCustomer = document.getElementsByName(supplierCustomer);
    
    type             = GMisc.firstElement(type);
    supplierCustomer = GMisc.firstElement(supplierCustomer);
    
    switch(type.value){
    
      case this._TYPE_IN:
      
        supplierCustomer.innerHTML = this._CUSTOMER;
        break;
        
      case this._TYPE_OUT:
      
        supplierCustomer.innerHTML = this._SUPPLIER;
        break;
      
      default:
        supplierCustomer.innerHTML = this._UNKNOWN;
    }
  },

  sayHello : function(){

    alert("Misc Linked");
  }
} 